package edu.neu.swc.skynet.filter;

import edu.neu.swc.skynet.constant.GatewayConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * <h1>缓存全局过滤器</h1>
 * 缓存请求Body的全局过滤器
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class GlobalCacheRequestBodyFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        boolean isLoginOrRegister =
                exchange.getRequest().getURI().getPath().contains(GatewayConstant.LOGIN_URI)
                        || exchange.getRequest().getURI().getPath().contains(GatewayConstant.REGISTER_URI);

        if(null == exchange.getRequest().getHeaders().getContentType() || ! isLoginOrRegister){
            return chain.filter(exchange);
        }

        //DataBufferUtils.join 拿到请求中的数据 --> DataBuffer
        return DataBufferUtils.join(exchange.getRequest().getBody()).flatMap(dataBuffer -> {
            //确保数据缓冲区不被释放，
            DataBufferUtils.retain(dataBuffer);
            // defer,just 都是去创建数据源，得到当前数据的副本
            Flux<DataBuffer> cacheFlux = Flux.defer(()->
                    Flux.just(dataBuffer.slice(0,dataBuffer.readableByteCount())));

            //重新包装ServerHTTPRequest，重写getBody方法，能够返回
            ServerHttpRequest mutatedRequest =
                    new ServerHttpRequestDecorator(exchange.getRequest()){
                        @Override
                        public Flux<DataBuffer> getBody() {
                            return cacheFlux;
                        }
                    };
            //将包装之后的ServerHTTPRequest向下传递
            return chain.filter(exchange.mutate().request(mutatedRequest).build());
        });
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE + 1;
    }
}
